package com.auth.controller;

import com.auth.model.BsSystemMenus;
import com.auth.service.MenuServiceI;
import com.exception.bean.Result;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

/**
 * <dl>
 * <dt>MenuController</dt>
 * <dd>Description: 菜单控制层</dd>
 * <dd>CreateDate: 2017/8/21</dd>
 * </dl>
 *
 * @author luo
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    @Autowired
    private MenuServiceI menuServiceI;

    /**
     * 删除菜单功能项
     *
     * @param id     标示ID
     * @param unitid 单位ID
     * @param userId 用户ID (只有超级管理员有权限删除)
     * @return
     */
    @PostMapping(value = "/deleteItems")
    public Result deleteMenusItemById(@NotEmpty(message = "标示ID不能为空") @Length(max = 50, min = 1) String id,
                                      @NotEmpty(message = "单位ID不能为空") @Length(max = 50, min = 1) String unitid,
                                      @NotEmpty(message = "所属的系统管理员ID不能为空") @Length(max = 50, min = 1) String userId) {
        return menuServiceI.deleteMenusItemById(id, unitid, userId);
    }

    /**
     * 新增菜单功能项
     *
     * @param mid    功能项ID
     * @param name   功能相名称
     * @param unitid 单位ID
     */
    @PostMapping(value = "/addItems")
    public Result addMenusItem(@NotEmpty(message = "所属的系统功能菜单项ID不能为空") @Length(max = 50, min = 1) String mid,
                               @NotEmpty(message = "功能项名称不能为空") @Length(max = 50, min = 1) String name,
                               @NotEmpty(message = "单位ID不能为空") @Length(max = 50, min = 1) String unitid) {
        return menuServiceI.addMenusItem(mid, name, unitid);
    }

    /**
     * 修改功能项信息
     *
     * @param id     标示ID
     * @param mid    功能项ID
     * @param name   功能项名称
     * @param unitid 单位ID
     */
    @PostMapping(value = "/updateItems")
    public Result updateMenuItemMenusIdOrNameById(@NotEmpty(message = "菜单ID不能为空") String id,
                                                  String mid, String name,
                                                  @NotEmpty(message = "单位ID不能为空") @Length(max = 50, min = 1) String unitid) {
        return menuServiceI.updateMenusItemMenuIdOrNameById(id, mid, name, unitid);
    }

    /**
     * 菜单功能项分页查询(对内)
     *
     * @param pageNo   页数
     * @param pageSize 行数
     * @param unitid   单位ID
     * @return
     */
    @PostMapping(value = "/findMenusItemsPage")
    public Result findMenusItemsPageByCondition(@RequestParam(required = true, defaultValue = "1") int pageNo,
                                                @RequestParam(required = true, defaultValue = "1") int pageSize,
                                                String unitid) {
        return menuServiceI.findMenusItemsPageByCondition(pageNo, pageSize, unitid);
    }

    /**
     * 菜单功能项查询(对外)
     *
     * @param unitid 单位ID
     * @param mid    菜单ID
     * @return
     */
    @PostMapping(value = "/findMenusItems")
    public Result findMenusItemsByCondition(@NotEmpty(message = "单位ID不能为空") String unitid,
                                            @NotEmpty(message = "菜单ID不能为空") String mid) {
        return menuServiceI.findMenusItemsByCondition(unitid, mid);
    }

    /**
     * 添加菜单
     *
     * @param unitid 单位ID
     * @param name   功能菜单名称
     * @param pid    上级功能菜单ID
     * @param type   应用类型（APP、PC）
     * @return
     */
    @PostMapping(value = "/addMenus")
    public Result addMenus(@NotEmpty(message = "单位ID不能为空") @Length(max = 50, min = 1) String unitid,
                           @NotEmpty(message = "菜单名称不能为空") @Length(max = 50, min = 1) String name,
                           String pid, @RequestParam(required = true, defaultValue = "0") Integer type) {
        return menuServiceI.addMenus(unitid, name, pid, type);
    }

    /**
     * 删除菜单
     *
     * @param id     菜单ID
     * @param userId 管理员ID
     * @param unitid 单位ID
     * @return
     */
    @PostMapping(value = "/deleteMenus")
    public Result deleteMenusById(@NotEmpty(message = "标示ID不能为空") @Length(max = 50, min = 1) String id,
                                  @NotEmpty(message = "所属的系统管理员ID不能为空") @Length(max = 50, min = 1) String userId,
                                  @NotEmpty(message = "单位ID不能为空") @Length(max = 50, min = 1) String unitid) {
        return menuServiceI.deleteMenusById(id, unitid, userId);
    }

    /**
     * 修改菜单名称
     *
     * @param id     菜单ID
     * @param unitid 单位ID
     * @param name   菜单名称
     * @return
     */
    @PostMapping(value = "/updateMenus")
    public Result updateMenusNameById(@NotEmpty(message = "标示ID不能为空") @Length(max = 50, min = 1) String id,
                                      @NotEmpty(message = "单位ID不能为空") @Length(max = 50, min = 1) String unitid,
                                      @NotEmpty(message = "菜单名称不能为空") @Length(max = 50, min = 1) String name) {
        return menuServiceI.updateMenusNameById(id, unitid, name);
    }

    /**
     * 根据条件查菜单(对外)
     *
     * @param id     菜单ID
     * @param unitid 单位ID
     * @param pid    上级功能菜单ID
     * @param type   应用类型（APP、PC）
     * @return
     */
    @PostMapping(value = "/findMenus")
    public Result findMenusByCondition(String id, @NotEmpty(message = "单位ID不能为空") String unitid, String pid, Integer type) {
        return menuServiceI.findMenusByCondition(id, unitid, pid, type);
    }

    /**
     * 分页查询菜单(对内)
     *
     * @param pageNo   页数
     * @param pageSize 行数
     * @param menus    参数
     * @return
     */
    @PostMapping(value = "/findMenusPage")
    public Result findMenusPageByCondition(@RequestParam(required = true, defaultValue = "1") int pageNo,
                                           @RequestParam(required = true, defaultValue = "1") int pageSize, BsSystemMenus menus) {
        return menuServiceI.findMenusPageByCondition(pageNo, pageSize, menus);
    }

    /**
     * 菜单功能项关联详细信息查询(对外)
     *
     * @param id     菜单ID
     * @param mid
     * @param unitid 单位ID
     * @return
     */
    @PostMapping(value = "/findItemsInfo")
    public Result findItemsInfoByCondition(@NotEmpty(message = "菜单ID不能为空") String id,String mid,
                                           @NotEmpty(message = "单位ID不能为空") String unitid) {
        return menuServiceI.findItemsInfoByCondition(id, unitid);
    }
}
